package com.ruoyi.framework.web.service;


import com.ruoyi.common.constant.VipConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.YxUserVipStatuses;
import com.ruoyi.system.domain.YxVipLevels;
import com.ruoyi.system.domain.YxVipRechargeOptions;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.IYxUserVipStatusesService;
import com.ruoyi.system.service.IYxVipLevelsService;
import com.ruoyi.system.service.IYxVipRechargeOptionsService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.apache.http.util.TextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备通知栏消息服务类
 */
@Component
public class VipRechargeService {

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private IYxVipRechargeOptionsService yxVipRechargeOptionsService;

    @Autowired
    private IYxVipLevelsService yxVipLevelsService;

    @Autowired
    private IYxUserVipStatusesService yxUserVipStatusesService;

    @Autowired
    private ISysConfigService sysConfigService;


    /**
     * 会员信息查询
     * @return
     */
    public R userVipStatusR(){
        return R.ok(userVipStatusList());

    }
    /**
     * 会员信息查询
     * @return
     */
    public List<YxUserVipStatuses> userVipStatusList(){
        // 增加会员升级判断
        SysUser user = SecurityUtils.getLoginUser().getUser();
        YxUserVipStatuses query = new YxUserVipStatuses();
        query.setUserId(user.getUserId());
        query.setExpirationDate(DateUtils.getNowDate());
        List<YxUserVipStatuses> userVipStatusesList = yxUserVipStatusesService.selectYxUserVipStatusesList(query);
        return userVipStatusesList;

    }

    /**
     * 支付页面 充值选项
     * 返回值包含{
     *     userVipStatuses 用户会员等级
     *     rechargeOptionsList 用户充值选项
     *     vipLevelsList 会员等级
     * }
     * 1、无会员默认展示 vip 和 svip
     * 2、有vip 场景则进行 增加svip升级选项
     * @return
     */
    public R selectYxVipRechargeOptionsList(){


        SysUser user = SecurityUtils.getLoginUser().getUser();

        Map retrunMap = new HashMap<>();

        YxUserVipStatuses queryVipStatus = new YxUserVipStatuses();
        queryVipStatus.setUserId(user.getUserId());


        // 1、vip等级信息展示
        List<YxVipLevels> vipLevelsList = yxVipLevelsService.selectYxVipLevelsList(new YxVipLevels());
        retrunMap.put("vipLevelsList", vipLevelsList);

        // 2、vip充值信息获取
        List<YxVipRechargeOptions> vipRechargeOptionsList = yxVipRechargeOptionsService.selectYxVipRechargeOptionsList(new YxVipRechargeOptions());

        // 判断会员是否有升级
        YxUserVipStatuses query = new YxUserVipStatuses();
        query.setUserId(user.getUserId());

        // 需要查询二个会员信息 对比计算升级费用
        query.setExpirationDate(DateUtils.getNowDate());
        List<YxUserVipStatuses> userVipStatusesList = yxUserVipStatusesService.selectYxUserVipStatusesList(query);
        Map<Long,YxUserVipStatuses> userVipStatusesMap = new HashMap();
        for(YxUserVipStatuses userVipStatuses1 : userVipStatusesList){
            userVipStatusesMap.put(userVipStatuses1.getVipLevelId(),userVipStatuses1);
        }



        if(userVipStatusesMap.containsKey(VipConstants.VIP_LEVEL_VIP)) {

            int day = 0;
            if(userVipStatusesMap.containsKey(VipConstants.VIP_LEVEL_SVIP)){

                if(userVipStatusesMap.get(VipConstants.VIP_LEVEL_SVIP).getExpirationDate().equals(userVipStatusesMap.get(VipConstants.VIP_LEVEL_VIP).getExpirationDate())){
                    retrunMap.put("rechargeOptionsList", vipRechargeOptionsList);
                    return R.ok(retrunMap);
                }
                day = DateUtils.differentDaysByMillisecond(userVipStatusesMap.get(VipConstants.VIP_LEVEL_SVIP).getExpirationDate(),userVipStatusesMap.get(VipConstants.VIP_LEVEL_VIP).getExpirationDate()) + 1;
            }else {
                day = DateUtils.differentDaysByMillisecond(DateUtils.getNowDate(),userVipStatusesMap.get(VipConstants.VIP_LEVEL_VIP).getExpirationDate()) + 1;
            }

            // 有 升级 计算出升级费用 并动态生成一个升级选项
            // 计算剩余天数

            // 动态计算升级费用 按照升级一个天 0.33元 计算。
            YxVipRechargeOptions yxVipRechargeOptionsUpgrade = new YxVipRechargeOptions();
            yxVipRechargeOptionsUpgrade.setVipLevel(VipConstants.VIP_LEVEL_SVIP);
            // 设置特定id标识是vip升级
            yxVipRechargeOptionsUpgrade.setId(VipConstants.RECHARGE_OPTION_UPGRADE_ID);
            yxVipRechargeOptionsUpgrade.setTimeString("升级SVIP");
            yxVipRechargeOptionsUpgrade.setDiscountTip("升级"+day+"天VIP");

            String vipUpgradePrivce = sysConfigService.selectConfigByKey("sys.pay.vipUpgradePrivce");
            if(TextUtils.isEmpty(vipUpgradePrivce)){
                vipUpgradePrivce = VipConstants.VIP_UPGRADE_PRIVCE_DEFAULT;
            }

            //将升级放入到第一个选项
            yxVipRechargeOptionsUpgrade.setPrice(new BigDecimal(vipUpgradePrivce).multiply(BigDecimal.valueOf(day)));
            vipRechargeOptionsList.add(0,yxVipRechargeOptionsUpgrade);

        }

        retrunMap.put("rechargeOptionsList", vipRechargeOptionsList);

        return R.ok(retrunMap);
    }

}
